/**
 * @description 图片处理
 */

import { translateApp, translateBrowser } from '@/components/libs/imager.js'
import storeService from './store-service.js'

// 图片压缩
export const compress = async function ({ src, quality } = {}) {
  let translate = translateBrowser
  // #ifdef APP-PLUS
  translate = translateApp
  // #endif
  let item = await storeService.getImgMaxWidth()
  let maxWidth = (item || {}).configValue
  maxWidth = parseFloat(maxWidth)
  maxWidth = isNaN(maxWidth) ? 1706 : maxWidth
  item = await storeService.getImgMaxHeight()
  let maxHeight = (item || {}).configValue
  maxHeight = parseFloat(maxHeight)
  maxHeight = isNaN(maxHeight) ? 1280 : maxHeight
  item = await storeService.getImgMinSize()
  let maxSize = (item || {}).configValue
  maxSize = parseFloat(maxSize)
  maxSize = isNaN(maxSize) ? 1024 : maxSize
  maxSize = maxSize * 1024

  return new Promise((resolve, reject) => {
    // 压缩
    translate({
      src, quality, maxSize, maxWidth, maxHeight
    }, (text) => {
      resolve(text)
    }, (e) => {
      reject(e)
    })
  })
}

